<?php
/**
 * 本页仅供测试
 */
class UploadAction extends Action {
    public function index() {
           $this->display();

    }
    public function fileDownload(){
        $this->display();
    }
    
    public function handleUploadExercise(){
        if(!IS_POST){
            $this->error('页面不存在');
        }
        /*========上传图片=============*/
        if (!empty($_FILES)) {

                /*=========整理上传图片信息===========*/
            $numQuestionPic = count($_FILES['question']['name']);  //题目的数量
            $numAnalysisPic = count($_FILES['analysis']['name']);  //解析的数量
            $_FILES['analysis']['name'][0] != '' ? $existAnalysis = ture : $existAnalysis = false ;

            /*=====上传的题目数量要与答案数量一致============*/
            $numQuestionPic == strlen(I('right_answer')) || $this->error('图片的数量与答案的数量不一致');
            if($existAnalysis) //
                $numQuestionPic == $numAnalysisPic || $this->error('图片的数量与解析的数量不一致');

            /*================将图片上传至domain===============*/
            import("@.ORG.UploadFile");
            $config=array(
                'allowExts'=>array('jpg','gif','png','jpeg'),
                // 'savePath'=>'./Public/upload/',
                'savePath'=>'./Public/upload/'.I('type').'/',
                // 'saveRule'=>'time',
            );
            $upload = new UploadFile($config);
            $upload->imageClassPath="@.ORG.Image";
            $upload->maxSize  = 9145728 ;// 设置附件上传大小
            $upload->thumb=true;
            $upload->thumbMaxHeight=300;
            $upload->thumbMaxWidth=200;
            if (!$upload->upload()) {
                $this->error($upload->getErrorMsg());
            }else{
                $info = $upload->getUploadFileInfo();
            }
        }

        /*============将上传的图片信息整理成一个二维数组===========*/
        //分两种情况，有解析，没有解析
        if($existAnalysis){
            for($i = 0 ; $i < $numQuestionPic ; $i++){
                $uploadExercise[$i] = array(
                'type' => I('type'),
                'questionPicPath' => '/Public/upload/'.I('type').'/',
                'questionPicName' => $info[$i]['savename'] ,
                'rightAnswer' => substr(I('right_answer'), $i,1) ,   //get each answer of input
                'analysisPicPath' => '/Public/upload/'.I('type').'/',
                'analysisPicName' => $info[$i+count($info)/2]['savename'],
                'time' => date('Y-m-d H:i:s'),
                );
            }
        }else{
            for($i = 0 ; $i < $numQuestionPic ; $i++){
                $uploadExercise[$i] = array(
                'type' => I('type'),
                'questionPicPath' => '/Public/upload/'.I('type').'/',
                'questionPicName' => $info[$i]['savename'] ,
                'rightAnswer' => substr(I('right_answer'), $i,1) ,   //get each answer of input
                'time' => date('Y-m-d H:i:s'),
                );
            }
        }

        /*===============将试题信息存入数据库==========*/
        if(M('questionbank')->addAll($uploadExercise)){
            $this->success('success in adding');
        }else{
            $this->error('上传失败');
        }
        
    }
    function forceDownload($filename) { 
        if (false == file_exists($filename)) { 
            return false; 
        } 
         
        // http headers 
        header('Content-Type: application-x/force-download'); 
        header('Content-Disposition: attachment; filename="' . basename($filename) .'"'); 
        header('Content-length: ' . filesize($filename)); 
     
        // for IE6 
        if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) { 
            header('Cache-Control: no-cache, must-revalidate'); 
        } 
        header('Pragma: no-cache'); 
             
        // read file content and output 
        return readfile($filename);; 
    } 

}